<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>mqtt体验</title>
    <style type="text/css">
      * {
        padding: 0;
        margin: 0;
      }
      html,
      body {
        width: 100%;
        height: 100%;
      }
      #container {
        width: 1600px;
        height: 1000px;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <!-- <script src="https://unpkg.com/vue-baidu-map"></script> -->
    <script
      type="text/javascript"
      src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=zh93Hg2gyEY5AasPrGMl6Y6EnbHUfq97"
    ></script>
    <script src="https://unpkg.com/vue-baidu-map"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
    <script type="text/javascript">
      $(function () {
        // 创建地图实例
        let map = new BMapGL.Map("container", {
          enableRotate: false,
          enableTilt: false,
        })

        let lat = 116.404
        let lng = 39.915

        // 创建点坐标
        let point = new BMapGL.Point(lat, lng)
        // 初始化地图，设置中心点坐标和地图级别
        map.centerAndZoom(point, 15)

        map.setHeading(50) //设置地图旋转角度
        map.setTilt(50) //设置地图的倾斜角度
        // 禁止地图旋转和倾斜可以通过配置项进行设置

        let scaleCtrl = new BMapGL.ScaleControl() // 添加比例尺控件
        map.addControl(scaleCtrl)
        let zoomCtrl = new BMapGL.ZoomControl() // 添加缩放控件
        map.addControl(zoomCtrl)
        let cityCtrl = new BMapGL.CityListControl() // 添加城市列表控件
        map.addControl(cityCtrl)

        let myIcon = new BMapGL.Icon("./car.png", new BMapGL.Size(23, 25))

        let marker = new BMapGL.Marker(point, { icon: myIcon }) // 创建标注
        map.addOverlay(marker)
        map.setMapStyle({
          styleId: "693769c37e701bb0a709e0113fbf7f8d",
        })

        // setInterval(() => {
        //   console.log(1)
        //   lat++
        //   lng++
        // }, 1000)

        const options = {
          clean: true,
          connectTimeout: 4000,
          clientId: "emqx_client_h5",
          // username: "admin",
          // password: "public",
        }

        const client = mqtt.connect("ws://101.35.101.111:8083/mqtt", options)

        //成功连接后触发的回调
        client.on("connect", function (connack) {
          console.log("成功连接上服务器...")
          //订阅某主题
          /**
           * client.subscribe(topic/topic array/topic object, [options], [callback])
           * topic:一个string类型的topic或者一个topic数组,也可以是一个对象
           * options
           */
          client.subscribe("car/position/bmw", { qos: 2 })
          //每隔2秒发布一次数据
          // setInterval(publish, 2000)
          publish()
        })

        function publish() {
          //发布数据
          /**
           * client.publish(topic,message,[options], [callback])
           *
           * message: Buffer or String
           * options:{
           * qos:0, //默认0
           * retain:false, //默认false
           * dup:false, //默认false
           * properties:{}
           * }
           * callback:function (err){}
           */
          const message = "message---" + new Date().getTime()
          client.publish("car/position/bmw", message, { qos: 2 })
          console.log("客户端发布了数据:" + message)
        }

        //当客户端接收到发布消息时触发回调
        /**
         * topic:收到的数据包的topic
         * message:收到的数据包的负载playload
         * packet:收到的数据包
         */
        client.on("message", (topic, message, packet) => {
          console.log(
            "客户端收到订阅消息,topic=" +
              topic +
              ";消息数据:" +
              message +
              ";数据包:" +
              JSON.stringify(packet)
          )
        })

        //当重新连接启动触发回调
        client.on("reconnect", () => {
          console.log("正在重连.....")
        })

        //连接断开后触发的回调
        client.on("close", function () {
          console.log("客户端已断开连接.....")
        })

        //从broker接收到断开连接的数据包后发出。MQTT 5.0特性
        client.on("disconnect", function (packet) {
          console.log("从broker接收到断开连接的数据包....." + packet)
        })

        //客户端脱机下线触发回调
        client.on("offline", function () {
          console.log("客户端脱机下线.....")
        })

        //当客户端无法连接或出现错误时触发回调
        client.on("error", error => {
          console.log("客户端出现错误....." + error)
        })

        //当客户端发送任何数据包时发出。这包括published()包以及MQTT用于管理订阅和连接的包
        client.on("packetsend", packet => {
          console.log("客户端已发出数据包....." + packet)
        })

        //当客户端接收到任何数据包时发出。这包括来自订阅主题的信息包以及MQTT用于管理订阅和连接的信息包
        client.on("packetreceive", packet => {
          console.log("客户端已收到数据包....." + packet)
        })

        //页面离开自动断开连接
        $(window).bind("beforeunload", () => {
          console.log("客户端窗口关闭,断开连接")
          client.disconnect()
        })
      })
    </script>
  </body>
</html>
